﻿Imports Microsoft.Practices.Composite.Regions
Imports Infragistics.Windows.Ribbon


Namespace Services


    Public Class NavigationService
        Implements INavigationService


        Private ReadOnly m_regionManager As IRegionManager

        Public Sub New(ByVal regionManager As IRegionManager)
            m_regionManager = regionManager
        End Sub

#Region " Register "

        Public Sub RegisterMenuTool(ByVal newRegionName As String, ByVal Caption As String, Optional ByVal addToRegionName As String = RegionNames.Shell_RibbonApplicationMenu) Implements INavigationService.RegisterMenuTool
            If Not m_regionManager.Regions.ContainsKey(newRegionName) Then

                'Get the ribbon region
                Dim region As IRegion = m_regionManager.Regions(addToRegionName)
                Dim MenuTool As New MenuTool
                MenuTool.Caption = Caption
                MenuTool.ButtonType = MenuToolButtonType.DropDown
                RegisterMenuTool(newRegionName, MenuTool, addToRegionName)
            End If
        End Sub

        Public Sub RegisterMenuTool(ByVal newRegionName As String, ByVal MenuTool As MenuTool, Optional ByVal addToRegionName As String = RegionNames.Shell_RibbonApplicationMenu) Implements INavigationService.RegisterMenuTool
            If Not m_regionManager.Regions.ContainsKey(newRegionName) Then
                'Get the ribbon region
                Dim region As IRegion = m_regionManager.Regions(addToRegionName)
                region.Add(MenuTool)
                ' Defign the inner region
                m_regionManager.AttachNewRegion(MenuTool, newRegionName)
            End If
        End Sub

        Public Sub RegisterRibbonGroup(ByVal newRegionName As String, ByVal Caption As String, Optional ByVal addToRegionName As String = RegionNames.Shell_Ribbon_HomeTab) Implements INavigationService.RegisterRibbonGroup
            If Not m_regionManager.Regions.ContainsKey(newRegionName) Then
                Dim tsrgServer As New RibbonGroup
                tsrgServer.Caption = Caption
                RegisterRibbonGroup(newRegionName, tsrgServer, addToRegionName)
            End If
        End Sub

        Public Sub RegisterRibbonGroup(ByVal newRegionName As String, ByVal RibbonGroup As RibbonGroup, Optional ByVal addToRegionName As String = RegionNames.Shell_Ribbon_HomeTab) Implements INavigationService.RegisterRibbonGroup
            If Not m_regionManager.Regions.ContainsKey(newRegionName) Then
                Dim region As IRegion = m_regionManager.Regions(addToRegionName)
                region.Add(RibbonGroup)
                m_regionManager.AttachNewRegion(RibbonGroup, newRegionName)
            End If
        End Sub

        Public Sub RegisterTabRegion(ByVal newRegionName As String, ByVal Header As String, Optional ByVal addToRegionName As String = RegionNames.Shell_Ribbon) Implements INavigationService.RegisterTabRegion
            If Not m_regionManager.Regions.ContainsKey(newRegionName) Then
                'Get the ribbon region
                Dim region As IRegion = m_regionManager.Regions(addToRegionName)
                ' Create a new toolbar for Team System
                Dim tsrti As New RibbonTabItem
                tsrti.Header = Header
                RegisterTabRegion(newRegionName, tsrti, addToRegionName)
            End If
        End Sub

        Public Sub RegisterTabRegion(ByVal newRegionName As String, ByVal RibbonTabItem As RibbonTabItem, Optional ByVal addToRegionName As String = RegionNames.Shell_Ribbon) Implements INavigationService.RegisterTabRegion
            If Not m_regionManager.Regions.ContainsKey(newRegionName) Then
                'Get the ribbon region
                Dim region As IRegion = m_regionManager.Regions(addToRegionName)
                ' Add the Tab as a regionable region and get the region manager for it.
                region.Add(RibbonTabItem, String.Format("{0}View", newRegionName))
                ' Defign the inner region
                m_regionManager.AttachNewRegion(RibbonTabItem, newRegionName)
            End If
        End Sub

        Public Sub RegisterContextualTabGroupRegion(ByVal newRegionName As String, ByVal Caption As String, Optional ByVal addToRegionName As String = RegionNames.Shell_Ribbon) Implements INavigationService.RegisterContextualTabGroupRegion
            If Not m_regionManager.Regions.ContainsKey(newRegionName) Then
                ' Create a new toolbar 
                Dim tsrti As New ContextualTabGroup
                tsrti.Caption = Caption
                tsrti.IsVisible = True
                ' Add the Tab as a regionable region and get the region manager for it.
                RegisterContextualTabGroupRegion(newRegionName, tsrti, addToRegionName)
            End If
        End Sub


        Public Sub RegisterContextualTabGroupRegion(ByVal newRegionName As String, ByVal ContextualTab As Infragistics.Windows.Ribbon.ContextualTabGroup, Optional ByVal addToRegionName As String = "Shell_Ribbon") Implements INavigationService.RegisterContextualTabGroupRegion
            If Not m_regionManager.Regions.ContainsKey(newRegionName) Then
                'Get the ribbon region
                Dim region As IRegion = m_regionManager.Regions(addToRegionName)
                ' Add the Tab as a regionable region and get the region manager for it.
                region.Add(ContextualTab, String.Format("{0}View", newRegionName))
                ' Defign the inner region
                m_regionManager.AttachNewRegion(ContextualTab, newRegionName)
            End If
        End Sub

        Public Sub EnsureBaseRegions() Implements INavigationService.EnsureBaseRegions
            RegisterTabRegion(RegionNames.Shell_Ribbon_HomeTab, "Home")
            RegisterRibbonGroup(RegionNames.Shell_Ribbon_HomeTab_HomeGroup, "Home", RegionNames.Shell_Ribbon_HomeTab)
            RegisterRibbonGroup(RegionNames.Shell_Ribbon_HomeTab_CommunicationsGroup, "Communications", RegionNames.Shell_Ribbon_HomeTab)
            RegisterRibbonGroup(RegionNames.Shell_Ribbon_HomeTab_DashboardsGroup, "Dashboards", RegionNames.Shell_Ribbon_HomeTab)
        End Sub

#End Region

#Region " Add "

        Public Sub AddSeparatorTool() Implements INavigationService.AddSeparatorTool
            AddTool(New SeparatorTool, RegionNames.Shell_RibbonApplicationMenu)
        End Sub

        Public Sub AddButtonTool(ByVal Caption As String, ByVal Command As System.Windows.Input.ICommand, ByVal CommandParam As Object, ByVal addToRegionName As String) Implements INavigationService.AddButtonTool
            Dim newTool As New ButtonTool
            newTool.Caption = Caption
            newTool.Command = Command
            newTool.CommandParameter = CommandParam
            AddTool(newTool, addToRegionName)
        End Sub

        Public Sub AddLabelTool(ByVal Caption As String, ByVal addToRegionName As String) Implements INavigationService.AddLabelTool
            Dim newTool As New LabelTool
            newTool.Caption = Caption
            AddTool(newTool, addToRegionName)
        End Sub

        Public Sub AddRadioButtonTool(ByVal Caption As String, ByVal IsChecked As Boolean, ByVal Command As System.Windows.Input.ICommand, ByVal CommandParam As Object, ByVal addToRegionName As String) Implements INavigationService.AddRadioButtonTool
            Dim newTool As New RadioButtonTool
            newTool.Caption = Caption
            newTool.Command = Command
            newTool.IsChecked = IsChecked
            newTool.CommandParameter = CommandParam
            AddTool(newTool, addToRegionName)
        End Sub

#End Region

        Public Sub AddTool(ByVal buttonTool As Infragistics.Windows.Ribbon.ButtonTool, ByVal addToRegionName As String) Implements INavigationService.AddTool
            m_regionManager.Regions(addToRegionName).Add(buttonTool)
        End Sub

        Public Sub AddTool(ByVal labelTool As Infragistics.Windows.Ribbon.LabelTool, ByVal addToRegionName As String) Implements INavigationService.AddTool
            m_regionManager.Regions(addToRegionName).Add(labelTool)
        End Sub

        Public Sub AddTool(ByVal radioButtonTool As Infragistics.Windows.Ribbon.RadioButtonTool, ByVal addToRegionName As String) Implements INavigationService.AddTool
            m_regionManager.Regions(addToRegionName).Add(radioButtonTool)
        End Sub

        Public Sub AddTool(ByVal separatorTool As Infragistics.Windows.Ribbon.SeparatorTool, ByVal addToRegionName As String) Implements INavigationService.AddTool
            m_regionManager.Regions(addToRegionName).Add(separatorTool)
        End Sub


        Public Sub AddTool(ByVal control As System.Windows.UIElement, ByVal addToRegionName As String) Implements INavigationService.AddTool
            m_regionManager.Regions(addToRegionName).Add(control)
        End Sub
    End Class

End Namespace